package cn.sykj.car.controller;


import cn.sykj.car.common.ResponseBean;
import cn.sykj.car.common.StatusCode;
import cn.sykj.car.dict.DeleteFlagEnum;
import cn.sykj.car.entity.ConfigBankAccount;
import cn.sykj.car.entity.Users;
import cn.sykj.car.service.IConfigBankAccountService;
import cn.sykj.car.service.IUsersService;
import cn.sykj.car.utils.Utils;
import cn.sykj.car.vo.ConfigBankAccountVO;
import cn.sykj.car.vo.ResponseMessageVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 * 银行账户管理 前端控制器
 * </p>
 *
 * @author xuming
 * @since 2021-06-24
 */
@RestController
@RequestMapping("/configBankAccount")
@Slf4j
public class ConfigBankAccountController extends BaseController {

    @Resource
    private IConfigBankAccountService iConfigBankAccountService;

    @Resource
    private IUsersService iUsersService;

    DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 添加银行账户信息
     *
     * @param configBankAccount
     * @param
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "添加银行账户信息", notes = "添加银行账户信息")
    @ApiImplicitParams({})
    @RequiresAuthentication
    @RequiresPermissions("configBankAccount:create")
    @RequestMapping(value = "/addConfigBankAccount", method = RequestMethod.POST)
    public ResponseBean addConfigBankAccount(@RequestBody ConfigBankAccount configBankAccount) throws Exception {

        // 获取登陆系统用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        log.debug((String) obj);

        Users user = iUsersService.getUserByName((String) obj);

        configBankAccount.setCreateTime(new Date());
        configBankAccount.setCreateUser(user.getUserName());
        configBankAccount.setUpdateTime(new Date());
        configBankAccount.setUpdateUser(user.getUserName());
        configBankAccount.setDelFlag(DeleteFlagEnum.FALSE.VALUE);

        ResponseMessageVO responseMessageVO = iConfigBankAccountService.addConfigBankAccount(configBankAccount);
        if (Utils.notEmpty(responseMessageVO) && responseMessageVO.isRetFlag()) {
            this.writeLog2Manager(responseMessageVO);
        }
        return new ResponseBean(StatusCode.OK, responseMessageVO);
    }

    /**
     * 删除银行账户信息
     *
     * @param id
     * @param
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "删除银行账户信息", notes = "删除银行账户信息")
    @ApiImplicitParams({})
    @RequiresAuthentication
    @RequiresPermissions("configBankAccount:delete")
    @RequestMapping(value = "/{id}/delete", method = RequestMethod.POST)
    public ResponseBean deleteConfigBankAccount(@PathVariable Integer id) throws Exception {

        // 获取登陆系统用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        log.debug((String) obj);

        ResponseMessageVO responseMessageVO = iConfigBankAccountService.deleteConfigBankAccount(id);
        if (Utils.notEmpty(responseMessageVO) && responseMessageVO.isRetFlag()) {
            this.writeLog2Manager(responseMessageVO);
        }
        return new ResponseBean(StatusCode.OK, responseMessageVO);
    }

    /**
     * 查询银行账户信息
     *
     * @param configBankAccountVO
     * @param currentPage
     * @param pageSize
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "查询银行账户信息", notes = "查询银行账户信息")
    @ApiImplicitParams({})
    @RequiresAuthentication
    @RequiresPermissions("configBankAccount:view")
    @RequestMapping(value = "/pageConfigBankAccount", method = RequestMethod.POST)
    public ResponseBean pageConfigBankAccount(@RequestBody ConfigBankAccountVO configBankAccountVO,
                                              @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer currentPage,
                                              @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize
    ) throws Exception {


        // 获取登陆系统用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        log.debug((String) obj);

        //分页查询全部记录
        if (pageSize == 0) {
            pageSize = Integer.MAX_VALUE;
        }

        IPage<ConfigBankAccount> page = new Page(currentPage, pageSize);
        QueryWrapper<ConfigBankAccount> queryWrapper = new QueryWrapper<>();
        queryWrapper.select(ConfigBankAccount.ID,ConfigBankAccount.MAIN_BANK_NAME,ConfigBankAccount.BRANCH_BANK_NAME,
                ConfigBankAccount.MAIN_BANK_NAME,ConfigBankAccount.USER_NAME,ConfigBankAccount.USER_ACCOUNT,ConfigBankAccount.REMARKS,
                ConfigBankAccount.UPDATE_TIME,ConfigBankAccount.UPDATE_USER);
//        queryWrapper.select("id,main_bank_name,branch_bank_name,main_bank_code,user_name,user_account,remarks,update_time,update_user");

        if (Utils.notEmpty(configBankAccountVO.getMainBankName())) {
            queryWrapper.like(ConfigBankAccount.MAIN_BANK_NAME, configBankAccountVO.getMainBankName());
        }

        if (Utils.notEmpty(configBankAccountVO.getUserName())) {
            queryWrapper.like(ConfigBankAccount.USER_NAME, configBankAccountVO.getUserName());
        }

        if (Utils.notEmpty(configBankAccountVO.getUpdateTimeStart())) {
            // 四位年-两位月-两位日 两位小时：两位分钟：两位秒
            queryWrapper.ge(ConfigBankAccount.UPDATE_TIME, sdf.parse(configBankAccountVO.getUpdateTimeStart()));
        }
        if (Utils.notEmpty(configBankAccountVO.getUpdateTimeEnd())) {
            queryWrapper.le(ConfigBankAccount.UPDATE_TIME, sdf.parse(configBankAccountVO.getUpdateTimeEnd()));
        }
        queryWrapper.orderByDesc(ConfigBankAccount.UPDATE_TIME);

        page = iConfigBankAccountService.page(page, queryWrapper);
        return new ResponseBean(StatusCode.OK, page);
    }

    /**
     * 修改银行账户信息
     *
     * @param configBankAccount
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "修改银行账户信息", notes = "修改银行账户信息")
    @ApiImplicitParams({})
    @RequiresAuthentication
    @RequiresPermissions("configBankAccount:edit")
    @RequestMapping(value = "/updateConfigBankAccount", method = RequestMethod.POST)
    public ResponseBean updateConfigBankAccount(@RequestBody ConfigBankAccount configBankAccount) throws Exception {

        // 获取登陆系统用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        log.debug((String) obj);

        // 当前登录人的获取
        Users user = iUsersService.getUserByName((String) obj);
        configBankAccount.setUpdateTime(new Date());
        configBankAccount.setUpdateUser(user.getUserName());

        ResponseMessageVO responseMessageVO = iConfigBankAccountService.updateConfigBankAccount(configBankAccount);
        this.writeLog2Manager(responseMessageVO);
        return new ResponseBean(StatusCode.OK, responseMessageVO);
    }
}
